package com.cql.service.business.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cql.common.core.DO.TagDO;
import com.cql.common.core.DTO.TagDTO;
import com.cql.common.utils.StringUtils;
import com.cql.mapper.TagMapper;
import com.cql.service.business.TagService;
import com.cql.service.cache.CommonCacheService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author cql
 */
@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, TagDO> implements TagService {

    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private CommonCacheService commonCacheService;

    @Override
    public List<TagDTO> listTag() {
        /**
         * 先从redis中获取，再去数据库中获取
         */
        List<TagDTO> result = commonCacheService.listTag();
        if (StringUtils.isNotEmpty(result)) {
            return result;
        }

        List<TagDO> tagDOS = tagMapper.selectList(null);

        result = new ArrayList<>();

        for (TagDO tagDO : tagDOS) {
            TagDTO t = new TagDTO();
            BeanUtils.copyProperties(tagDO, t);
            result.add(t);
        }

        //存储进redis
        commonCacheService.insertAllTag(result);

        return result;
    }
}
